from flask_sqlalchemy import SQLAlchemy
from datetime import datetime
from passlib.hash import argon2

db = SQLAlchemy()



# ----------------- 用户表 -----------------
class User(db.Model):
    __tablename__ = 'user_db'
    user_id = db.Column(db.BigInteger, primary_key=True)
    user_name = db.Column(db.String(64), unique=True, nullable=False)
    password_hash = db.Column(db.String(255), nullable=False)
    phone_number = db.Column(db.String(32))
    membership_type = db.Column(db.Enum('member', 'premium'))
    subscription_end = db.Column(db.DateTime)
    create_at = db.Column(db.DateTime, default=datetime.now)
    
    def set_password(self, password):
        self.password_hash = argon2.hash(password)
    
    def check_password(self, password):
        return argon2.verify(password, self.password_hash)


# ----------------- 管理员表 -----------------
class Admin(db.Model):
    __tablename__ = 'admin_db'
    admin_id = db.Column(db.BigInteger, primary_key=True)
    admin_name = db.Column(db.String(64), unique=True, nullable=False)  
    admin_phone_number = db.Column(db.String(32))
    admin_right = db.Column(db.Enum('super', 'content', 'audit'))
    admin_password = db.Column(db.String(255), nullable=False) 

# ----------------- 管理员操作表 -----------------
class AdminAction(db.Model):
    __tablename__ = 'admin_action_db'
    admin_id = db.Column(db.BigInteger, db.ForeignKey('admin_db.admin_id'), primary_key=True)
    admin_operation = db.Column(db.Enum('upload', 'audit', 'delete'))
    operation_content = db.Column(db.Text)


# ----------------- 图片主表 -----------------
class Image(db.Model):
    __tablename__ = 'image_db'
    image_id = db.Column(db.String(64), primary_key=True)
    image_title = db.Column(db.String(255))
    image_description = db.Column(db.String(255))
    resolution = db.Column(db.String(16))
    format = db.Column(db.Enum('png', 'jpg', 'webp'))
    midjourney_code = db.Column(db.JSON)
    codecopy_time = db.Column(db.BigInteger)
    beliked = db.Column(db.BigInteger, default=0)       
    becollected = db.Column(db.BigInteger, default=0)
    comment_time = db.Column(db.BigInteger, default=0)               
    comment_context = db.Column(db.Text)                
    vip_exclusive = db.Column(db.String(255))
             
    image_tag1 = db.Column(db.String(255))
    image_tag2 = db.Column(db.String(255))
    image_tag3 = db.Column(db.String(255))

# ----------------- 图片版本表 -----------------
class ImageVersion(db.Model):
    __tablename__ = 'image_version_db'
    version_id = db.Column(db.BigInteger, primary_key=True)
    image_id = db.Column(db.String(64), db.ForeignKey('image_db.image_id'))
    admin_id = db.Column(db.BigInteger, db.ForeignKey('admin_db.admin_id'))  
    audit_status = db.Column(db.Enum('pending', 'approved', 'rejected'))
    oss_path = db.Column(db.String(255))
    upload_at = db.Column(db.DateTime)
    vip_exclusive = db.Column(db.Enum('yes', 'no', name='vip_exclusive_types'))

# ----------------- 收藏夹表 -----------------
class UserCollect(db.Model):
    __tablename__ = 'usercollect_db'
    usercollect_id = db.Column(db.BigInteger, primary_key=True)
    user_id = db.Column(db.BigInteger, db.ForeignKey('user_db.user_id'), nullable=False)
    collect_name = db.Column(db.String(255))

# ----------------- 收藏项表 -----------------
class CollectItem(db.Model):
    __tablename__ = 'items_db'
    items_id = db.Column(db.BigInteger, primary_key=True)
    items_name = db.Column(db.String(255))
    usercollect_id = db.Column(db.BigInteger, db.ForeignKey('usercollect_db.usercollect_id'), nullable=False)
    image_id = db.Column(db.String(64), db.ForeignKey('image_db.image_id'))

# ----------------- 用户行为表 -----------------
class UserAction(db.Model):
    __tablename__ = 'user_action_db'
    action_id = db.Column(db.BigInteger, primary_key=True)
    user_id = db.Column(db.BigInteger, db.ForeignKey('user_db.user_id'), nullable=False)
    action_type = db.Column(db.Enum('collect','collect_item', 'comment', 'like'))
    image_id = db.Column(db.String(64), db.ForeignKey('image_db.image_id'))
    comment_context = db.Column(db.Text)
    create_at = db.Column(db.DateTime, default=datetime.now)


# ----------- VIP用户表 -----------
class UserVIP(db.Model):
    __tablename__ = 'user_vip_db'
    user_id = db.Column(db.BigInteger, db.ForeignKey('user_db.user_id'), primary_key=True)
    vip_status = db.Column(db.Enum('member', 'premium', name='vip_status_types'), default='member')
    start_date = db.Column(db.DateTime)
    end_date = db.Column(db.DateTime)
    renewal_count = db.Column(db.Integer, default=0)
    total_duration = db.Column(db.Integer, default=0)  
    last_renewal_date = db.Column(db.DateTime)

    user = db.relationship('User', backref=db.backref('vip_info', uselist=False))


#   VIP套餐表模型
class VIPPackage(db.Model):
    __tablename__ = 'vip_package'
    package_id = db.Column(db.Integer, primary_key=True)
    package_type = db.Column(db.String(50), unique=True, nullable=False)  # 如：monthly
    duration_days = db.Column(db.Integer, nullable=False)
    original_price = db.Column(db.Numeric(10, 2), nullable=False)
    discount_rate = db.Column(db.Numeric(3, 2), default=0)  # 0-1之间的小数
    is_discount_active = db.Column(db.Boolean, default=False)
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
